package com.cat.filter;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author :HYC  email: kobe524348@gmail.com
 * @program : HYC_CatRecruit
 * @name : BaseFilter
 * @description : a filter to check all request
 * @create : 2019-03-26 20:25
 */
@WebFilter(
        filterName = "EncodingFilter",
        urlPatterns = {"/*"}, servletNames = {"/*"},
        initParams = {
        @WebInitParam(name = "ENCODING", value = "UTF-8")
}
)
public class EncodingFilter implements Filter {
    private String ENCODING =null;

    public void init(FilterConfig config){
            this.ENCODING= config.getInitParameter("ENCODING");
    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("do encoding filter");
            HttpServletRequest req  = (HttpServletRequest) servletRequest;
            HttpServletResponse res = (HttpServletResponse)servletResponse;
            req.setCharacterEncoding(ENCODING);
            filterChain.doFilter(servletRequest,servletResponse);
    }
}
